package com.yc.exception;

import com.yc.web.controller.model.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    // 处理文件上传异常，例如总大小超限
    @ExceptionHandler(FileSizeLimitExceededException.class)
    public ResponseEntity<ResponseResult> handleFileSizeLimitExceededException(FileSizeLimitExceededException ex) {
        logger.error("文件大小超限异常：{}", ex.getMessage(), ex);
        return ResponseEntity.status(ex.getHttpStatus()).body(ResponseResult.error(ex.getMessage()));
    }

    // 处理不支持的文件类型异常
    @ExceptionHandler(InvalidFileTypeException.class)
    public ResponseEntity<ResponseResult> handleInvalidFileTypeException(InvalidFileTypeException ex) {
        logger.error("文件类型不支持异常：{}", ex.getMessage(), ex);
//        );
        return ResponseEntity.status(ex.getHttpStatus()).body(ResponseResult.error(ex.getMessage()));
    }

    // 处理所有未捕获的通用异常，作为兜底
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseResult> handleGenericException(Exception ex) {
        logger.error("通用异常：{}", ex.getMessage(), ex);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ResponseResult.error(ex.getMessage()));
    }
}